// pages/order/order.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    total_price: '',
    captionCol: false,
    orderSubmitCol: false,
    name: '',
    phone: '',
    lastTime: 0
  },

  onLoad: function(options) {
    var that = this;
    if (options.share_id) {
      this.setData({
        share_id: options.share_id
      });
    }else{
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  },

  onShow:function(){
    var that=this;
    setTimeout(function () {
      that.getGoodsInfo()
    }, 0)
  },


  getGoodsInfo: function() {
    var that = this;
    api.POST({
      params: {
        share_id: that.data.share_id
      },
      url: app.globalData.base_url + 'goods/shareInfo',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          if (res.data.data.status == 1) {
            wx.showModal({
              title: '提示',
              content: '该订单已被提交啦，无法再提交了~',
              showCancel: false,
              confirmText: '知道了',
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/index/index'
                  })
                }
              }
            })
            return false;
          }
          that.setData({
            orderInfo: res.data.data,
            items: [{
              sku_code: res.data.data.sku_code,
              num: res.data.data.num,
              gname: res.data.data.gname,
              current_price: res.data.data.share_price,
              image: res.data.data.image,
              cover: res.data.data.cover,
              product_detail_image: res.data.data.product_detail_image,
              volume: res.data.data.volume,
              volume_unit: res.data.data.volume_unit,
              degree: res.data.data.degree,
              degree_unit: res.data.data.degree_unit,
              unit: res.data.data.unit,
              total: (res.data.data.num * res.data.data.share_price).toFixed(2)
            }],
            total_price: (res.data.data.num * res.data.data.share_price).toFixed(2),
          })
        } else {
          util.showError(res.data.message)
        }
      },
      fail: function() {
        util.showError("网络错误，请检查网络");
      }
    });
  },

  //输入名字
  nameInput: function(e) {
    var name = e.detail.value;
    name = name.replace(/(^\s*)|(\s*$)/g, "");
    this.setData({
      name: name
    })
  },

  //输入电话
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  submitConfirm: function(e) {
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    var that = this;
    if (that.data.name == '') {
      util.showError("请输入您的姓名");
      return false;
    }

    if (that.data.phone == '') {
      util.showError("请输入您的联系电话");
      return false;
    }else{
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(that.data.phone))) {
        util.showError("手机号不正确");
        return false;
      }
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交当前订单',
      success(res) {
        if (res.confirm) {
          that.payOrder()
        }
      }
    })
  },

  // 提交订单
  payOrder: function() {
    util.showLoading("数据加载中..");
    var params = new Object()
    var that = this;
    var list = [];
    params.record = [];
    for (var i = 0; i < this.data.items.length; i++) {
      var box = {};
      box.sku_code = this.data.items[i].sku_code
      box.num = this.data.items[i].num
      list.push(box);
    }
    params.record = JSON.stringify(list);
    params.username = that.data.name;
    params.mobile = that.data.phone;
    params.by_shopping_cart = '0';
    params.share_id = that.data.share_id;


    if (this.data.orderSubmitCol) {
      // util.showError("请勿重复提交订单");
      return false
    } else {
      this.setData({
        orderSubmitCol: true
      })
    }

    api.POST({
      params: params,
      url: app.globalData.base_url + 'order/submit',
      success: function(res) {
        util.hiddenLoading();
        if (util.isEmpty(res.data.code)) {
          var oid = res.data.data.result.oid;
          wx.redirectTo({
            url: '/agent/pages/groupOrderSuccess/groupOrderSuccess?oid=' + oid
          })
        } else {
          if (res.data.code == 11828) {
            wx.showModal({
              title: '提示',
              content: '该订单已被提交啦，无法再提交了~',
              showCancel: false,
              confirmText: '知道了'
            });
          } else {
            util.showStatus(res.data.message);
          }
        }
      },
      fail: function(fail) {
        util.hiddenLoading();
        util.showError("网络错误，请检查网络");
      }
    })
  },

   goLink: function () {
      wx.switchTab({
         url: '/pages/index/index'
      });
   },

})